package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.UserAddrMapper;
import com.qf.pojo.ResultData;
import com.qf.pojo.UserAddr;
import com.qf.service.UserAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class UserAddrServiceImp implements UserAddrService {

    @Autowired(required = false)
    private UserAddrMapper userAddrMapper;

    @Override
    public ResultData<UserAddr> findUserAddrByUserId(String userId) {
        QueryWrapper<UserAddr> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);

        final List<UserAddr> addrList = userAddrMapper.selectList(queryWrapper);
        if (addrList != null) {
            return new ResultData(0, "查询成功", addrList);
        }

        return new ResultData<>(10000, "查询失败，请检查参数是否有误");
    }

    @Override
    public ResultData<UserAddr> deleteUserAddrById(String addrId) {
        final int count = userAddrMapper.deleteById(addrId);
        if (count>0){
            return new ResultData<>(0,"删除成功");

        }
        return new ResultData<>(10000,"删除失败");
    }

    @Override
    public ResultData<UserAddr> updateUserAddr(UserAddr userAddr) {
            if(userAddr != null){
                final int count = userAddrMapper.updateById(userAddr);
                if (count>0){
                    return new ResultData<>(0,"修改成功");
                }else {
                    return new ResultData<>(10000,"修改失败");
                }
            }

        return new ResultData<>(10000,"请检查参数是否有误");
    }

    @Override
    public ResultData<UserAddr> insertUserAddr(UserAddr userAddr) {
        if (userAddr != null ){
            final int count = userAddrMapper.insert(userAddr);
            if (count>0){
                return new ResultData<>(0,"添加成功");
            }else {
                return new ResultData<>(10000,"添加失败");
            }
        }
        return new ResultData<>(1000,"请检查传入的参数是否有误");
    }
}
